//
//  RedPacketViewController.m
//  fxxiaoke
//
//  Created by 鞠超 on 16/9/26.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "RedPacketViewController.h"

#import "RedPacketTableViewCell.h"
#import "RedPacketDetailViewController.h"

#define NSStringFromFormat(fmt,...)     [NSString stringWithFormat:fmt,##__VA_ARGS__]

@interface RedPacketViewController () <UITableViewDelegate, UITableViewDataSource>
//顶部视图
@property (nonatomic, strong) UIView * view_Top;

@property (nonatomic, strong) UIButton * btn_Receive;
@property (nonatomic, strong) UIButton * btn_Send;
@property (nonatomic, strong) UIView * view_Slide;

@property (nonatomic, assign) BOOL is_Receive;

@property (nonatomic, strong) UITableView * tableView;
//头视图
@property (nonatomic, strong) UIView * view_HeadReceive;
@property (nonatomic, strong) UIView * view_HeadSend;

@property (nonatomic, strong) UIImageView * image_head;
@property (nonatomic, strong) UILabel * name_head;
@property (nonatomic, strong) UILabel * price_head;

@property (nonatomic, strong) UILabel * lbl_ReceiveNumber;
@property (nonatomic, strong) UILabel * lbl_ReceiveBest;
@property (nonatomic, strong) UILabel * lbl_SendNumber;

@end

@implementation RedPacketViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_TopView];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    
    _lblTitle.text = @"我的红包";
    //250 94 98
    _topView.backgroundColor = [UIColor colorWithRed:250/255.0 green:94/255.0 blue:98/255.0 alpha:1.0f];
    
    //左返回
    UIImageView * image_LeftRetrun = [[UIImageView alloc] initWithFrame:CGRectMake(11, 31.5, 25, 25)];
    image_LeftRetrun.userInteractionEnabled = YES;
    image_LeftRetrun.image = [UIImage imageNamed:@"Left-return.png"];
    [_topView addSubview:image_LeftRetrun];
    
    UILabel * lbl_return = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_LeftRetrun.frame) - 6, 35, 35, 15)];
    lbl_return.text = @"返回";
    lbl_return.textColor = [UIColor whiteColor];
    lbl_return.font = [UIFont systemFontOfSize:17];
    [_topView addSubview:lbl_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 22, 60, 40);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + 50, SCREEN_WIDTH, SCREEN_HEIGHT - 114) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] init];
    //头视图
    [self p_headView];
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[RedPacketTableViewCell class] forCellReuseIdentifier:@"cell_Receive"];
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 11;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RedPacketTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_Receive" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if(self.is_Receive == 0)
    {//收到的红包界面
        
        cell.detail.hidden = YES;
        
        //
        cell.image.hidden = NO;
        CGFloat length_x = [@"拼手气红包" boundingRectWithSize:CGSizeMake(1000, 20) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17.0f]} context:nil].size.width;
        cell.image.frame = CGRectMake(length_x + 10 + 5, 25, 20, 20);
    }
    else
    {//发出的红包界面
        
        cell.detail.hidden = NO;

        cell.image.hidden = YES;
    }
    
    return cell;
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.is_Receive == 0)
    {//收到的红包界面
        NSLog(@"收到的红包界面");
    }
    else
    {//发出的红包界面
        NSLog(@"发出的红包界面");
    }
    
    RedPacketDetailViewController * redPacketDetailViewController = [[RedPacketDetailViewController alloc] init];
    [self showViewController:redPacketDetailViewController sender:nil];
}

#pragma mark - 头视图
- (void )p_headView
{
    if(self.is_Receive == 1)
    {
        self.view_HeadReceive = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 285)];
        self.view_HeadReceive.backgroundColor = [UIColor whiteColor];
        self.tableView.tableHeaderView = self.view_HeadReceive;
        
        //头像
        self.image_head = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 40, 25, 80, 80)];
        self.image_head.layer.cornerRadius = 40;
        self.image_head.layer.masksToBounds = YES;
        self.image_head.image = [UIImage imageNamed:@"heads.jpg"];
        [self.view_HeadReceive addSubview:self.image_head];
        
        //名字
        self.name_head = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(self.image_head.frame) + 20, SCREEN_WIDTH - 20, 15)];
        self.name_head.text = @"员工姓名";
        self.name_head.textAlignment = NSTextAlignmentCenter;
        self.name_head.font = [UIFont systemFontOfSize:17.0f];
        [self.view_HeadReceive addSubview:self.name_head];
        
        //红包数
        self.price_head = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(self.name_head.frame) + 15, SCREEN_WIDTH - 20, 30)];
        self.price_head.text = @"￥29.00";
        self.price_head.textColor = [UIColor colorWithRed:250/255.0 green:94/255.0 blue:98/255.0 alpha:1.0f];
        self.price_head.font = [UIFont systemFontOfSize:36.0f];
        self.price_head.textAlignment = NSTextAlignmentCenter;
        [self.view_HeadReceive addSubview:self.price_head];
        
        //发出的个数
        self.lbl_SendNumber = [[UILabel alloc] initWithFrame:CGRectMake(10, 270 - 15 - 15, SCREEN_WIDTH - 20, 15)];
        self.lbl_SendNumber.font = [UIFont systemFontOfSize:17.0f];
        self.lbl_SendNumber.textAlignment = NSTextAlignmentCenter;
        [self.view_HeadReceive addSubview:self.lbl_SendNumber];
        
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:NSStringFromFormat(@"发出红包4个")];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:250/255.0 green:94/255.0 blue:98/255.0 alpha:1.0f] range:NSMakeRange(4,1)];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0,4)];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(5,1)];
        self.lbl_SendNumber.attributedText = str;
        
        //
        UIView * view_bg = [[UIView alloc] initWithFrame:CGRectMake(0, 270, SCREEN_WIDTH, 15)];
        view_bg.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self.view_HeadReceive addSubview:view_bg];
    }
    else
    {
        self.view_HeadSend = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 285)];
        self.view_HeadSend.backgroundColor = [UIColor whiteColor];
        self.tableView.tableHeaderView = self.view_HeadSend;

        //头像
        self.image_head = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 40, 25, 80, 80)];
        self.image_head.layer.cornerRadius = 40;
        self.image_head.layer.masksToBounds = YES;
        self.image_head.image = [UIImage imageNamed:@"heads.jpg"];
        [self.view_HeadSend addSubview:self.image_head];
        
        //名字
        self.name_head = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(self.image_head.frame) + 20, SCREEN_WIDTH - 20, 15)];
        self.name_head.text = @"员工姓名-XX";
        self.name_head.textAlignment = NSTextAlignmentCenter;
        self.name_head.font = [UIFont systemFontOfSize:17.0f];
        [self.view_HeadSend addSubview:self.name_head];
        
        //红包金额
        self.price_head = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(self.name_head.frame) + 15, SCREEN_WIDTH - 20, 30)];
        self.price_head.text = @"￥2229.00";
        self.price_head.textColor = [UIColor colorWithRed:250/255.0 green:94/255.0 blue:98/255.0 alpha:1.0f];
        self.price_head.font = [UIFont systemFontOfSize:36.0f];
        self.price_head.textAlignment = NSTextAlignmentCenter;
        [self.view_HeadSend addSubview:self.price_head];
        
        
        self.lbl_ReceiveNumber = [[UILabel alloc] initWithFrame:CGRectMake(0, 270 - 15 - 15 - 25, SCREEN_WIDTH / 2, 20)];
        self.lbl_ReceiveNumber.text = @"212";
        self.lbl_ReceiveNumber.textColor = [UIColor colorWithRed:250/255.0 green:94/255.0 blue:98/255.0 alpha:1.0f];
        self.lbl_ReceiveNumber.textAlignment = NSTextAlignmentCenter;
        self.lbl_ReceiveNumber.font = [UIFont systemFontOfSize:23.0];
        [self.view_HeadSend addSubview:self.lbl_ReceiveNumber];
        
        UILabel * lbl_ReceiveRed = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.lbl_ReceiveNumber.frame) + 10, SCREEN_WIDTH / 2, 15)];
        lbl_ReceiveRed.text = @"收到红包";
        lbl_ReceiveRed.textAlignment = NSTextAlignmentCenter;
        lbl_ReceiveRed.textColor = [UIColor grayColor];
        lbl_ReceiveRed.font = [UIFont systemFontOfSize:15.0f];
        [self.view_HeadSend addSubview:lbl_ReceiveRed];
        
        self.lbl_ReceiveBest = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2, 270 - 15 - 15 - 25, SCREEN_WIDTH / 2, 20)];
        self.lbl_ReceiveBest.text = @"12";
        self.lbl_ReceiveBest.textColor = [UIColor colorWithRed:250/255.0 green:94/255.0 blue:98/255.0 alpha:1.0f];
        self.lbl_ReceiveBest.textAlignment = NSTextAlignmentCenter;
        self.lbl_ReceiveBest.font = [UIFont systemFontOfSize:23.0];
        [self.view_HeadSend addSubview:self.lbl_ReceiveBest];
        
        UILabel * lbl_ReceiveBestRed = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2, CGRectGetMaxY(self.lbl_ReceiveBest.frame) + 10, SCREEN_WIDTH / 2, 15)];
        lbl_ReceiveBestRed.text = @"手气最佳";
        lbl_ReceiveBestRed.textAlignment = NSTextAlignmentCenter;
        lbl_ReceiveBestRed.textColor = [UIColor grayColor];
        lbl_ReceiveBestRed.font = [UIFont systemFontOfSize:15.0f];
        [self.view_HeadSend addSubview:lbl_ReceiveBestRed];
        
        //
        UIView * view_bg = [[UIView alloc] initWithFrame:CGRectMake(0, 270, SCREEN_WIDTH, 15)];
        view_bg.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self.view_HeadSend addSubview:view_bg];
    }
}

#pragma mark - 顶部视图
- (void )p_TopView
{
    //顶部视图
    self.view_Top = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 50)];
    self.view_Top.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.view_Top];
    
    //收到红包
    self.btn_Receive = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.btn_Receive.frame = CGRectMake(0, 0, SCREEN_WIDTH / 2, 50);
    [self.btn_Receive setTitle:@"收到的红包" forState:(UIControlStateNormal)];
    [self.btn_Receive setTitleColor:[UIColor colorWithRed:250/255.0 green:94/255.0 blue:98/255.0 alpha:1.0f] forState:(UIControlStateNormal)];
    self.btn_Receive.titleLabel.font = [UIFont systemFontOfSize:17.0f];
    [self.view_Top addSubview:self.btn_Receive];
    [self.btn_Receive addTarget:self action:@selector(btn_ReceiveAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //发出红包
    self.btn_Send = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.btn_Send.frame = CGRectMake(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, 50);
    [self.btn_Send setTitle:@"发出的红包" forState:(UIControlStateNormal)];
    [self.btn_Send setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    self.btn_Send.titleLabel.font = [UIFont systemFontOfSize:17.0f];
    [self.view_Top addSubview:self.btn_Send];
    [self.btn_Send addTarget:self action:@selector(btn_SendAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIView * view_Line = [[UIView alloc] initWithFrame:CGRectMake(0, 48, SCREEN_WIDTH, 2)];
    view_Line.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view_Top addSubview:view_Line];
    
    //顶部视图
    self.view_Slide = [[UIView alloc] init];
    self.view_Slide.frame = CGRectMake(0, 48, SCREEN_WIDTH / 2, 2);
    self.view_Slide.backgroundColor = [UIColor colorWithRed:250/255.0 green:94/255.0 blue:98/255.0 alpha:1.0f];
    [self.view_Top addSubview:self.view_Slide];
}

#pragma mark - 点击顶部按钮
- (void )btn_ReceiveAction:(UIButton *)sender
{
//    NSLog(@"点击顶部按钮,收到的红包");
    
    [UIView animateWithDuration:0.4f animations:^{
        
        self.view_Slide.frame = CGRectMake(0, 48, SCREEN_WIDTH / 2, 2);
        
        [self.btn_Receive setTitleColor:[UIColor colorWithRed:250/255.0 green:94/255.0 blue:98/255.0 alpha:1.0f] forState:(UIControlStateNormal)];
        [self.btn_Send setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    }];
    
    self.is_Receive = NO;
    [self p_headView];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tableView reloadData];
    });
}

- (void )btn_SendAction:(UIButton *)sender
{
//    NSLog(@"点击顶部按钮,发出的红包");
    
    [UIView animateWithDuration:0.4f animations:^{
        
        self.view_Slide.frame = CGRectMake(SCREEN_WIDTH / 2, 48, SCREEN_WIDTH / 2, 2);
        
        [self.btn_Send setTitleColor:[UIColor colorWithRed:250/255.0 green:94/255.0 blue:98/255.0 alpha:1.0f] forState:(UIControlStateNormal)];
        [self.btn_Receive setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    }];
    
    self.is_Receive = YES;
    [self p_headView];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tableView reloadData];
    });
}







@end
